﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using LyalinSamples.WP7Client.MaVHubInt;
using LyalinSamples.WP7Client.Themes;

namespace LyalinSamples.WP7Client
{
    public partial class MainPage : PhoneApplicationPage
    {
        #region Initialization
        public MainPage()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            DetectTheme();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            // Music and Video Hub: Check if it starting from the MaV hub
            if (this.NavigationContext != null &&
                this.NavigationContext.QueryString != null &&
                    this.NavigationContext.QueryString.Count > 0)
            {
                ParseZuneIntegrationParams(this.NavigationContext.QueryString);
            }

            base.OnNavigatedTo(e);
        }
        #endregion

        #region MaV
        private void ParseZuneIntegrationParams(IDictionary<string, string> queryStrings)
        {
            try
            {
                // Get the paramaters we original added to "PlayerContext"
                string MaVArea = queryStrings["Area"];

                // Clear query string now that you have its paramaters
                this.NavigationContext.QueryString.Clear(); // Prevents problem with going back ot MainPage after your done with episode

                // Navigate to our "player"
                // Important Note: You MUST resume playback when a user starts your application from Music and Video hub
                NavigationService.Navigate(new Uri("/MaVHubInt/MaVHubLandPage.xaml?Area=" + MaVArea, UriKind.Relative));
            }
            catch (System.Exception ex)
            {

                Application.Current.RootVisual.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("Error: For an unknown reason we cannot start the episode playback, exception: " + ex.ToString());
                });
            }
        }

        /// <summary>
        /// If your application played anything this needs to show it as the last thing played
        /// Note: this is required
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCreateNowPlayingIcon_Click(object sender, RoutedEventArgs e)
        {
            MaVHubIntegrator MaVIntegator = new MaVHubIntegrator();
            MaVIntegator.CreateImageInMaV(MaVAreas.NowPlaying);
        }

        /// <summary>
        /// If your application played any kind of audio or video content
        /// Note: this is required
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCreateHistoryIcon_Click(object sender, RoutedEventArgs e)
        {
            MaVHubIntegrator MaVIntegator = new MaVHubIntegrator();
            MaVIntegator.CreateImageInMaV(MaVAreas.History);
        }

        /// <summary>
        /// If your application created a new item, example: Downloaded music file or created a radio station within your application
        /// Note: this is optional if your application does NOT create anything new
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCreateNewIcon_Click(object sender, RoutedEventArgs e)
        {
            MaVHubIntegrator MaVIntegator = new MaVHubIntegrator();
            MaVIntegator.CreateImageInMaV(MaVAreas.New);
        }
        #endregion

        #region Theme Detection
        private void DetectTheme()
        {
            ThemeDetection detect = new ThemeDetection();

            textPhoneTheme.Text = detect.GetActiveTheme();
        }
        #endregion
    }
}